<?php
/**
 * MatchDart! is a darts event ranking table component for joomla
 * 
 * @version		2.0.1 | $Id: penalty.php 93 2009-08-28 10:56:02Z mwi.nor $
 * @package		MatchDart!
 * @subpackage	Admin
 * @copyright	Copyright (C) 2005 - 2009 Marco Wilde. All rights reserved.
 * @license		GNU/GPL, see LICENSE.php
 * @author		Marco Wilde, <mailto://info@matchdart.de>
 * @link		http://www.matchdart.de
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */

// no direct access
defined( '_JEXEC' ) or die( 'Restricted access' );

jimport( 'joomla.application.component.model' );

class MatchdartModelPenalty extends JModel
{
    protected $_data = null;
    protected $_id = null;
    protected $_eventselect = null;
    protected $_seasonselect = null;
    protected $_playerselect = null;
    protected $_teamselect = null;
    
    public function __construct()
    {
        parent::__construct();

        $array = JRequest::getVar('cid',  0, '', 'array');
        $this->setId((int)$array[0]);
    }

    public function setId($id)
    {
        // Set id and wipe data
        $this->_id        = $id;
        $this->_data    = null;
    }

    public function getData()
    {
        $app = &JFactory::getApplication();

        // Load the data
        if(empty( $this->_data )) {
            $query = ' SELECT * FROM #__md_penalty '.
                 '  WHERE PenaltyID = '.$this->_id;
            $this->_db->setQuery( $query );
            $this->_data = $this->_db->loadObject();
        }
        if (!$this->_data) {
            $this->_data = $this->getTable('penalties');
            $this->_data->id = 0;
        }
        return $this->_data;
    }

    public function store()
    {
        $row =& $this->getTable('penalties');
        $data = JRequest::get( 'post' );

        // Bind the form fields to the teams table
        if (!$row->bind($data)) {
            $this->setError($this->_db->getErrorMsg());
            return false;
        }

        // Make sure the team record is valid
        if (!$row->check()) {
            $this->setError($this->_db->getErrorMsg());
            return false;
        }

        // Store the entry to the database
        if (!$row->store()) {
            $this->setError($this->_db->getErrorMsg());
            return false;

        }
        return true;
    }

    public function delete()
    {
        $cids = JRequest::getVar( 'cid', array(0), 'post', 'array' );
        $row =& $this->getTable('penalties');

        foreach($cids as $cid) {
            if (!$row->delete( $cid )) {
                $this->setError( $row->_db->getErrorMsg() );
                return false;
            }
        }

        return true;
    }

    public function getEventselect()
    {
    	$penaltydata = $this->getData();
    	
    	if (empty($this->_eventselect))
    	{
    	$currentValue = $penaltydata->PenaltyEventID;
        $query = 'SELECT * FROM #__md_events ORDER BY EventID';
        $events = $this->_getList($query);

        $k=1;
        //FIXME: 'foreach' --> error if events table empty
        foreach ($events as $event){
        	$inactive = ($event->EventPublish == 0) ? 'true' : 'false';
        	$option[$k] = JHTML::_('select.option', $event->EventID, $event->EventName);
        	$k++;
        }
        
        $this->_eventselect = JHTML::_('select.genericlist', $option, 'PenaltyEventID', null, 'value', 'text', $currentValue);;
        
    	}
    	return $this->_eventselect;
    }

	public function getSeasonselect()
    {
    	$penaltydata = $this->getData();
    	    	
		if (empty($this->_seasonselect))
    	{
    	$currentValue = $penaltydata->PenaltySeasonID;
    	$query = 'SELECT * FROM #__md_seasons ORDER BY SeasonID';
        $seasons = $this->_getList($query);

        $k=1;
        //FIXME: 'foreach' --> error if seasons table empty
        foreach ($seasons as $season){
        	$inactive = ($season->SeasonPublish == 0) ? 'true' : 'false';
        	$option[$k] = JHTML::_('select.option', $season->SeasonID, $season->SeasonName);
        	$k++;
        }
        
        $this->_seasonselect = JHTML::_('select.genericlist', $option, 'PenaltySeasonID', null, 'value', 'text', $currentValue);;
        
    	}
    	return $this->_seasonselect;
    }
    
    public function getPlayerselect()
    {
    	$penaltydata = $this->getData();

    	if (empty($this->_playerselect))
    	{
    	$currentValue = $penaltydata->PenaltyPlayerID;
        $query = 'SELECT * FROM #__md_players ORDER BY PlayerName';
        $players = $this->_getList($query);

        $k=1;
        //FIXME: 'foreach' --> error if events table empty
        $option[0] = JHTML::_('select.option', '0', JText::_( 'MD_SELECT_NONE'));
        foreach ($players as $player){
        	$option[$k] = JHTML::_('select.option', $player->PlayerID, $player->PlayerName);
        	$k++;
        }
        
        $this->_playerselect = JHTML::_('select.genericlist', $option, 'PenaltyPlayerID', null, 'value', 'text', $currentValue);;
        
    	}
    	return $this->_playerselect;
    }
    
    public function getTeamselect()
    {
    	$penaltydata = $this->getData();
    	    	
    	if (empty($this->_teamselect))
    	{
    	$currentValue = $penaltydata->PenaltyTeamID;
		$query = 'SELECT * FROM #__md_teams ORDER BY TeamName';
        $teams = $this->_getList($query);

        $k=1;
        //FIXME: 'foreach' --> error if events table empty
        $option[0] = JHTML::_('select.option', '0', JText::_( 'MD_SELECT_NONE'));
        foreach ($teams as $team){
        	$option[$k] = JHTML::_('select.option', $team->TeamID, $team->TeamName);
        	$k++;
        }
        
        $this->_teamselect = JHTML::_('select.genericlist', $option, 'PenaltyTeamID', null, 'value', 'text', $currentValue);;
                        
    	}
    	return $this->_teamselect;
    }
    
}
?>